import { message } from 'antd';
export default {
  namespace: 'produce',

  state: {
    workingInfo: [], // 服务器上的用户信息
    btnStartClicked: false, // 按钮是否被按下
    errorInfo: '无提示消息', // 自动作业过程中的错误提示框
    errorInfoModalVisble: false, // 自动作业过程中的错误提示框是否展示
    sizeShowSrc: null, // 提示用的那个图片
    produceStartModalVisible: false,
    produceStartModalInfo: '无提示消息',
    resetModalVisible: false,
    resetModalInfo: '无提示消息',
    btnResetAllClicked: false,
    boardImageVisible: false, // 识别的提示的图
    boardImageSrc: null, // 识别异的图片
    boardImageInfo: '无提示消息', // 识别提示信息
    resetModalVisible: false, // 复位提示框
    resetModalInfo: null, // 复位提示信息
    configParams: [],
  },

  // 和后台交互
  effects: {
    *handleProduceMessage({ payload }, { call, put }) {
      const { msg } = payload;
      if (msg.val === 'workingInfo') {
        yield put({
          type: 'getWorkingInfo',
        });
      } else if (msg.val == 'opendooralarm') {
        yield put({
          type: 'opendooralarm',
          payload: msg.opendoorModalVisble,
        });
      } else if (msg.val == 'errorState') {
        yield put({
          type: 'updateErrorState',
          payload: [true, msg.errorInfo],
        });
      } else if (msg.val == 'boardImageState') {
        yield put({
          type: 'updateBoardImageModalState',
          payload: [msg.modalVisible, msg.boardImageSrc, msg.calcInfo],
        });
      } else if (msg.val == 'produceStartModal') {
        yield put({
          type: 'updateProduceStartModalState',
          payload: [msg.modalVisible, msg.produceStartInfo],
        });
      } else if (msg.val == 'resetAllModal') {
        yield put({
          type: 'updateResetModalState',
          payload: [msg.modalVisible, msg.resetInfo],
        });
      }
    },
    *testNewFunction({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_reqeust_test_new_function', []);
    },
    *reset({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_reset', []);
    },
    *saveWorkingInfo({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_reqeust_save_working_info', [
        payload,
      ]);
      if (response === 'ok') {
        message.success('保存成功!', 3);
      } else {
        message.error('保存失败,' + response, 5);
      }
      yield put({
        type: 'getWorkingInfo',
      });
    },

    *getWorkingInfo({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_get_working_info', []);
      yield put({
        type: 'updateWorkingInfo',
        payload: response,
      });
    },

    *clickChangeOrder({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_change_order', [payload]);
      if (response === 'ok') {
        message.success('更换工单成功!', 3);
      } else {
        message.error('更换工单失败!' + response, 5);
      }
    },
    *clickStartProduce({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_start_produce', [payload]);
      yield put({
        type: 'updateStartProduceBtn',
        payload: false,
      });
      if (response === 'ok') {
        message.success('自动作业启动成功!', 3);
      } else {
        message.error('自动作业启动失败,' + response, 5);
      }
    },

    *clickStopProduce({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_stop_produce', []);
      if (response === 'ok') {
        message.success('自动作业停止指令下发成功!', 3);
      } else {
        message.error('自动作业停止失败,' + response, 5);
      }
    },
    *clickResetAll({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_reset_all', []);
      yield put({
        type: 'updateResetAllBtnClick',
        payload: false,
      });
      if (response === 'ok') {
        message.success('复位成功!', 3);
      } else {
      }
    },

    *clickStopRasterAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_raster_alarm_reset', []);
      if (response === 'ok') {
        message.success('上料光栅状态解除指令下发成功!', 3);
      } else {
        message.error('上料光栅状态解除指令下发失败,' + response, 5);
      }
    },

    *closeProduceStartModal({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'ph',
        'web_request_close_produce_start_modal',
        []
      );
      yield put({
        type: 'updateProduceStartModalState',
        payload: [false, ''],
      });
    },
    *saveConfigParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cp', 'web_request_save_config_parms', [
        payload,
      ]);
      if (response === 'ok') {
        message.success('保存成功', 3);
      } else {
        message.error('保存失败,原因: ' + response, 8);
      }
      yield put({
        type: 'getConfigParams',
      });
    },

    *getConfigParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cp', 'web_request_get_config_params', []);
      yield put({
        type: 'updateConfigParams',
        payload: response,
      });
    },

    *clickResetCutterSelf({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_reset_cutter_self', []);
    },
    *saveGetImage({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_save_get_image', []);
    },
    *openImageLocation({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_open_image_location', []);
    },
    *saveGetColorImage({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_save_get_image_color', []);
    },
    *openColorImageLocation({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'ph',
        'web_request_open_color_image_location',
        []
      );
    },
    *clearLv1FullQuit({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_set_Lv1_full_quit', []);
      if (response === 0) {
        message.success('取出成功', 3);
      } else {
        message.error('取出失败,原因: ' + response, 8);
      }
    },
    *clearLv2FullQuit({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_set_Lv2_full_quit', []);
      if (response === 0) {
        message.success('取出成功', 3);
      } else {
        message.error('取出失败,原因: ' + response, 8);
      }
    },
    *clearGrateAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_clear_grate_alarm', []);
    },
    *clearPinFlag({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_set_place_start', [
        payload,
      ]);
      if (response === 0) {
        message.success('切换成功', 3);
      } else {
        message.error('切换失败,原因: ' + response, 8);
      }
    },
    *closeRaster({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_close_raster', [
        
      ]);
      if (response === 0) {
        message.success('屏蔽完成', 3);
      } else {
        message.error('切换失败,原因: ' + response, 8);
      }
    },
    *openRaster({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_open_raster', [
        
      ]);
      if (response === 0) {
        message.success('光栅启动', 3);
      } else {
        message.error('切换失败,原因: ' + response, 8);
      }
    },
    *setAGVOrArtificial({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_set_AGV_or_artificial', [
        payload,
      ]);
      if (response === 0) {
        message.success('下料模式切换成功', 3);
      } else {
        message.error('切换失败,原因: ' + response, 8);
      }
    },
  },

  // 状态更新
  reducers: {
    waitReturn(state, action) {
      return {
        ...state,
        isReturned: action.payload,
      };
    },
    updateResetAllBtnClick(state, action) {
      return {
        ...state,
        btnResetAllClicked: action.payload,
      };
    },
    updateConfigParams(state, action) {
      return {
        ...state,
        configParams: action.payload,
      };
    },
    opendooralarm(state, action) {
      return {
        ...state,
        opendoorModalVisble: action.payload,
      };
    },

    updateStartProduceBtn(state, action) {
      return {
        ...state,
        btnStartClicked: action.payload,
      };
    },
    updateProduceStartModalState(state, action) {
      return {
        ...state,
        produceStartModalVisible: action.payload[0],
        produceStartModalInfo: action.payload[1],
      };
    },
    updateBoardImageModalState(state, action) {
      let showImage;
      if (action.payload[1] != null) {
        showImage = 'data:image/jpeg;base64,' + action.payload[1];
      } else {
        showImage = null;
      }
      return {
        ...state,
        boardImageVisible: action.payload[0],
        boardImageSrc: showImage,
        boardImageInfo: action.payload[2],
      };
    },
    updateResetModalState(state, action) {
      return {
        ...state,
        resetModalVisible: action.payload[0],
        resetModalInfo: action.payload[1],
      };
    },

    updateErrorState(state, action) {
      return {
        ...state,
        errorInfoModalVisble: action.payload[0],
        errorInfo: action.payload[1],
      };
    },
    closeErrorInfoModal(state, action) {
      return {
        ...state,
        errorInfoModalVisble: false,
        errorInfo: '',
      };
    },
  },
};
